package com.bupt.config.interceptor.thread;

import com.bupt.dto.sys.LoginUserDto;
import com.bupt.dto.sys.UserDto;
import org.springframework.stereotype.Component;

import java.util.function.Consumer;
import java.util.function.Supplier;


/**
 * 存储用当前操作的用户
 * 根据执行的线程来做
 */
@Component
public class CurrentUserInfo {
    // 存储当前的用户的操作
    private static final ThreadLocal<UserDto> UserDtoThreadLocal = new ThreadLocal<>();
    // 获取当前的登陆用户
   /* public static final Supplier<UserDto> getCurrentOptUser = UserDtoThreadLocal::get;
    public static final Consumer<UserDto> putCurrentUser = UserDtoThreadLocal;*/

    // 获取当前的登陆用户
    public static UserDto getCurrentOptUser() {
        return UserDtoThreadLocal.get();
    }

    // 设置当前的登陆用户
    public static void putCurrentUser(UserDto UserDto) {
        UserDtoThreadLocal.set(UserDto);
    }

    // 移除当前的用户 防止出现 内存泄漏
    public static void removeCurrentLoginUser() {
        UserDtoThreadLocal.set(null);
        UserDtoThreadLocal.remove();
    }
}
